<template>
  <div class="app-container">
    <el-dialog
      v-model="props.visible"
      title="内文生产单选择"
      width="71%"
      @close="handleCancel"
    >
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="auto"
      >
        <el-row class="flex-box">
          <div>
            <el-form-item label="产品名称" prop="produceName">
              <el-input
                v-model.trim="queryParams.produceName"
                clearable
                style="width: 150px"
                placeholder="请输入产品名称"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item label="生产单编码" prop="produceNo">
              <el-input
                v-model.trim="queryParams.produceNo"
                clearable
                style="width: 150px"
                placeholder="请输入生产单编码"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </div>
          <div>
            <el-button
              class="btn-box"
              type="primary"
              plain
              @click="handleSubmit"
              :disabled="multiple"
            >
              确认
            </el-button>
          </div>
        </el-row>
      </el-form>
      <div>
        <el-row :gutter="20" style="overflow: auto">
          <el-col :span="12">
            <el-table
              ref="dataListRef"
              v-loading="loading"
              :data="dataList"
              stripe
              @row-click="handleClick"
              highlight-current-row
              border
              style="cursor: pointer"
            >
              <el-table-column
                label="生产单编码"
                min-width="150"
                align="center"
                prop="produceNo"
                :show-overflow-tooltip="true"
              >
              </el-table-column>
              <el-table-column
                label="产品名称"
                min-width="200"
                align="center"
                prop="productName"
                :show-overflow-tooltip="true"
              >
              </el-table-column>
              <!-- <el-table-column
                label="加急交货日期"
                min-width="120"
                align="center"
                :show-overflow-tooltip="true"
                prop="urgentDate"
              >
                <template v-slot="{ row }">
                  {{ parseTime(row.urgentDate) ?? parseTime(row.deliverDate) }}
                </template>
              </el-table-column> -->
              <!-- <el-table-column
                label="订单数量"
                min-width="200"
                align="center"
                prop="orderCount"
                :show-overflow-tooltip="true"
              >
                <template v-slot="{ row }">
                  <el-button type="primary" text>{{
                    row.orderCount
                  }}</el-button>
                </template>
              </el-table-column> -->
              <el-table-column
                label="定价"
                width="100"
                align="center"
                prop="fixPrice"
                :show-overflow-tooltip="true"
              >
                <template v-slot="{ row }">
                  <el-button type="primary" text>{{ row.fixPrice }}</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="12">
            <el-table
              ref="orderRef"
              v-loading="loadingInfo"
              :data="breedList"
              row-key="productId"
              stripe
              border
              style="cursor: pointer"
              @row-click="handleClickBreed"
            >
              <!-- @selection-change="handleSelectionChange" -->

              <!-- <el-table-column
                type="selection"
                :reserve-selection="true"
                width="55"
              /> -->
              <el-table-column
                label="品种"
                align="center"
                prop="breed"
              ></el-table-column>
              <el-table-column
                label="订单数量"
                align="center"
                prop="produceCount"
              ></el-table-column>
              <el-table-column
                label="预计用纸令数"
                align="center"
                prop="totalReamAmount"
              ></el-table-column>
              >
            </el-table>
          </el-col>
        </el-row>

        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-dialog>
  </div>
</template>


<script setup name="searchMaterialReceipt">
import { getProduceList } from "@/api/production/otherReport.js";
import { getProduceById } from "@/api/production/cover";
import { toNumber } from "@/utils/ruoyi";

const { proxy } = getCurrentInstance();
const emit = defineEmits(["changeUpdate"]);
const { mes_material_unit } = proxy.useDict("mes_material_unit");
const loading = ref(true);
const dataList = ref([]);
const selectRow = ref([]);
const multiple = ref(true);
const router = useRouter();
const dataListRef = ref(null);
const breedList = ref([]);
const loadingInfo = ref(false);
const total = ref(0);
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  otherReport: {
    type: [String, Number],
    default: "",
  },
  produceSelectList: {
    type: Array,
    default: () => {
      [];
    },
  },
});
watch(
  () => props.visible,
  (val) => {
    if (val) {
      handleQuery();
      nextTick(() => {
        dataListRef.value.clearSelection();
        if (props.produceSelectList.length > 0) {
          props.produceSelectList.forEach((item) => {
            dataListRef.value.toggleRowSelection(item, true);
          });
        }
      });
    }
  }
);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    status: 1,
  },
});

const { queryParams } = toRefs(data);

const getList = () => {
  loading.value = true;
  if (props.otherReport) {
    queryParams.value.otherReport = props.otherReport;
  }
  getProduceList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        dataListRef.value.setCurrentRow(res.rows[0]);
      }
      if (dataList.value.length > 0) {
        handleClick(dataList.value[0]);
      } else {
        breedList.value = [];
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

const handleClick = (row) => {
  loadingInfo.value = true;
  let otherReport = undefined;
  if (props.otherReport) {
    otherReport = props.otherReport;
  }
  getProduceById({ pid: row.produceId, otherReport }).then((res) => {
    console.log(res.data);
    res.data.produceProductList.forEach((item) => {
      item.packInNum = res.data.packInNum || 0;
      item.totalReamAmount = 0;
      item.orderCount = item.produceCount;
      item.produceNo = res.data.produceNo;
      item.productName = res.data.productName;
      res.data.producePrintList.forEach((ele) => {
        if (item.orderProductId == ele.orderProductId) {
          item.totalReamAmount += Number(ele.totalReamAmount);
          item.totalReamAmount = toNumber(item.totalReamAmount, 2);
        }
      });
    });
    // res.data.producePrintList.forEach((item) => {
    //   item.producePrintId = item.printId;
    //   item.produceNo = res.data.produceNo;
    //   item.createTime = undefined;
    //   res.data.produceProductList.forEach((ele) => {
    //     if (item.orderProductId == ele.orderProductId) {
    //       item.newPast = ele.newPast;
    //       item.breed = ele.breedType;
    //       item.bookName = ele.bookName;
    //       item.orderNo = ele.orderNo;
    //     }
    //   });
    // });
    breedList.value = res.data.produceProductList;
    loadingInfo.value = false;
  });
};
const handleCancel = () => {
  proxy.resetForm("queryRef");
  emit("cancel");
};

const handleSubmit = () => {
  emit("changeUpdate", selectRow.value);
};

//单选
const handleCellClick = (row) => {
  proxy.$modal
    .confirm("是否确认选择「" + row.produceNo + "」")
    .then(function () {
      emit("changeUpdate", row);
    })
    .catch(() => {});
};
const handleClickBreed = (row) => {
  proxy.$modal
    .confirm("是否确认选择「" + row.breed + "」")
    .then(function () {
      row.otherReport = props.otherReport;
      emit("changeUpdate", row);
    })
    .catch(() => {});
};
// 选中
const handleSelectionChange = (row) => {
  selectRow.value = row;
  multiple.value = !row.length;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
// getList()
</script>
<style lang="scss" scoped></style>